package com.jdy.haoduoai.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.jdy.haoduoai.LeXiaoXiaoBanApp;
import com.jdy.haoduoai.intface.IAsyncTask;
import com.jdy.haoduoai.model.LeUser;
import com.jdy.haoduoai.util.CustomAsyncTask;
import com.jdy.haoduoai.util.HttpUtils;
import com.jdy.haoduoai.util.ResponseResult;
import com.jdy.haoduoai.util.Tools;
import com.ycsj.goldmedalnewconcept.R;

import org.droidparts.annotation.inject.InjectView;

import java.util.HashMap;

//import com.jdy.haoduoai.R;

/**
 * Fill feedback editable box and upload to server.
 * 
 * @author zp @ 2015-6-8 下午3:11:51
 */
public class SettingFeedbackActivity extends BaseActivity {

  @InjectView(id = R.id.feedbackEditText)
  private EditText mFeedbackEditText;

  private LeUser mLeUser;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.setting_feedback_layout);

    setTitle("意见反馈");

    mUprightButton.setText("发表");

    initView();

    mFeedbackEditText.addTextChangedListener(new TextWatcher() {
      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {

      }

      @Override
      public void beforeTextChanged(CharSequence s, int start, int count,
          int after) {

      }

      @Override
      public void afterTextChanged(Editable s) {
        String tmpStr = s.toString().replaceAll(" ", "");
        if (Tools.isEmpty(tmpStr)) {
          mUprightButton.setTextColor(0xff858394);
        } else {
          mUprightButton.setTextColor(0xfff16122);
        }

      }
    });
  }

  private void initView() {
    mLeUser = LeXiaoXiaoBanApp.getInstance().getUser();
    mUprightButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (Tools.isNotEmpty(mFeedbackEditText.getText().toString()
            .replaceAll(" ", ""))) {
          new CustomAsyncTask(SettingFeedbackActivity.this, new IAsyncTask() {
            @Override
            public void onRecieveData(Activity activity, ResponseResult result) {
              if (result.isSuccess()) {
                toastShow("感谢您的及时反馈，我们马上去处理");
                finish();
                overridePendingTransition(R.anim.push_right_in,
                    R.anim.push_right_out);
              } else {
                if (Tools.isNotNullStr(result.data)) {
                  toastShow(result.data);
                }
              }
            }

            @Override
            public ResponseResult doInbackground(Activity activity) {
              LeUser lu = ((SettingFeedbackActivity) activity).mLeUser;
              String sno = lu.sno;
              HashMap<String, Object> param_ = new HashMap<String, Object>();
              param_.put(HttpUtils.TAG_SNO_I, sno);
              param_.put("comment", mFeedbackEditText.getText().toString());
              param_.put(HttpUtils.TAG_KEY_I, HttpUtils.KEY);

              return HttpUtils.startRequest(HttpUtils.API_URL
                  + HttpUtils.FEEDBACK_METHOD_POST, param_,
                  HttpUtils.TAG_POST_REQUEST);
            }
          }).execute();
        } else {
          Toast
              .makeText(getApplicationContext(), "请输入反馈内容!", Toast.LENGTH_LONG)
              .show();
        }

      }
    });
  }
}
